//
//  LaunchView.m
//  Common
//
//  Created by guoshiqing on 2018/5/29.
//

#import "LaunchView.h"
#import "UIView+DDAddition.h"
#import "Macro.h"
#import "UIImageView+WebCache.h"
#import "EPhotoWall.h"
#import "PhotoViewerVC.h"

@interface LaunchView() <UIScrollViewDelegate>
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIButton *btnEnter;
@property (nonatomic, strong) UIImageView *igv;

@property (nonatomic, strong) NSArray *imageNames;
@property (nonatomic, assign) CGFloat space;

@property (nonatomic, weak) BaseVC *parentVC;
@end

@implementation LaunchView

- (instancetype)initWithFrame:(CGRect)frame andImageNames:(NSArray <NSString *> *)imageNames withSpace:(CGFloat)space andParentVC:(BaseVC *)parentVC
{
    if (self = [super initWithFrame:frame]) {
        self.frame = frame;
        self.space = space;
        self.imageNames = imageNames;
        self.parentVC = parentVC;
        [self addSubviews];
    }
    return self;
}

- (void)addSubviews
{
    [self addSubview:self.scrollView];
    [self addSubview:self.pageControl];
}

- (void)showImg:(UITapGestureRecognizer *)tap
{
//    NSUInteger index = btn.tag;
    
    UIImageView *igv = (UIImageView *)tap.view;
    NSInteger index = igv.tag;

    PhotoViewerVC *photoVC = [[PhotoViewerVC alloc] init];
    photoVC.isTapDismissVC = YES;
    photoVC.index = index;
    photoVC.type = RESOURCE_NET;
    photoVC.dataList = self.imageNames;
    [self.parentVC.navigationController pushViewController:photoVC animated:YES];
}

#pragma mark - ScrollerView Delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//    self.pageControl.currentPage = scrollView.contentOffset.x / UI_SCREEN_WIDTH;
    
    NSInteger currentPage = scrollView.contentOffset.x / self.width;
    self.pageControl.currentPage = currentPage - 1;
    
    NSInteger numberOfPages = self.pageControl.numberOfPages + 1;
    if (currentPage == numberOfPages) {
        self.pageControl.currentPage = 0;
        self.scrollView.contentOffset = CGPointMake(self.width, 0);
    } else if (currentPage == 0) {
        self.pageControl.currentPage = numberOfPages - 1;
        self.scrollView.contentOffset = CGPointMake((numberOfPages - 1) * self.width , 0);
    }
}

#pragma mark - 懒加载
- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.bounces = NO;
        _scrollView.contentSize = CGSizeMake(self.width * self.imageNames.count, 0);
        
        NSMutableArray *arr = [[NSMutableArray array] init];
        arr = [self.imageNames mutableCopy];
        if (self.imageNames.count > 1) {
            NSString *first = self.imageNames[0];
            NSInteger count = self.imageNames.count - 1;
            NSString *end = self.imageNames[count];
            [arr insertObject:end atIndex:0];
            [arr addObject:first];
        }
        _scrollView.contentSize = CGSizeMake(self.width * arr.count, 0);
        if (self.imageNames.count != 1) {
            _scrollView.contentOffset = CGPointMake(self.width, 0);
        } else {
            _scrollView.contentOffset = CGPointMake(0, 0);
        }
        self.pageControl.numberOfPages = self.imageNames.count;
        
        for (NSInteger i = 0; i < arr.count; i ++) {
            [_scrollView addSubview:({
                self.igv = [[UIImageView alloc] initWithFrame:CGRectMake(self.space+UI_SCREEN_WIDTH*i, 0, self.width-2*self.space, _scrollView.height)];
                [self.igv sd_setImageWithURL:[NSURL URLWithString:arr[i]] placeholderImage:[UIImage imageNamed:@"default_png_345x230"]];
                self.igv.contentMode = UIViewContentModeScaleAspectFill;
                self.igv.clipsToBounds = YES;
                self.igv.tag = i;
                self.igv.userInteractionEnabled = YES;
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showImg:)];
                [self.igv addGestureRecognizer:tap];
                self.igv;
            })];
        }
    }
    return _scrollView;
}

- (UIPageControl *)pageControl
{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, self.width/2, 30.0f)];
        _pageControl.center = CGPointMake(self.width/2, self.height - Cell_X);
        _pageControl.numberOfPages = self.imageNames.count;
        _pageControl.userInteractionEnabled = NO;
    }
    return _pageControl;
}

- (void)setParentVC:(BaseVC *)parentVC
{
    _parentVC = parentVC;
}

@end
